<?php

namespace app\merchant\controller\content\files;

use app\merchant\controller\Base;
use app\merchant\model\UploadGroup as GroupModel;

/**
 * 文件分组
 * Class Group
 * @package app\merchant\controller\content
 */
class Group extends Base
{
    /**
     * 文件分组列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new GroupModel;
        $list = $model->getList();
        return $this->renderSuccess($list,"查询成功");
    }

    /**
     * 添加文件分组
     * @return array|mixed
     */
    public function add()
    {
        $model = new GroupModel;
        if ($model->add($this->postData())) {
            return $this->renderSuccess([],'添加成功');
        }
        return $this->renderError([],$model->getError() ?: '添加失败');
    }

    /**
     * 编辑文件分组
     * @param $group_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $param = $this->postData();

        if(!isset($param['group_id']) || isInt($param['group_id'])){
            return $this->renderError([],'参数错误');
        }

        //详情
        $model = GroupModel::detail($param['group_id']);
        if(!$model){
            return $this->renderError([],'该分组不存在，请刷新重试');
        }

        // 更新记录
        if ($model->edit($this->postData())) {
            return $this->renderSuccess([],'更新成功');
        }
        return $this->renderError([],$model->getError() ?: '更新失败');
    }

    /**
     * 文件分组详情
     * @param $group_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $param = $this->postData();

        if(!isset($param['group_id']) || isInt($param['group_id'])){
            return $this->renderError([],'参数错误');
        }

        //详情
        $model = GroupModel::detail($param['group_id']);
        if(!$model){
            return $this->renderError([],'该分组不存在，请刷新重试');
        }

        return $this->renderSuccess($model,'查询失败');
    }


    /**
     * 删除文件分组
     * @param $group_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $param = $this->postData();

        if(!isset($param['group_id']) || isInt($param['group_id'])){
            return $this->renderError([],'参数失败');
        }

        //详情
        $model = GroupModel::detail($param['group_id']);
        if(!$model){
            return $this->renderError([],'该分组不存在，请刷新重试');
        }
        if (!$model->remove()) {
            return $this->renderError([],$model->getError() ?: '删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }

}
